/******************************************************************************
 * Copyright 2023 The Apollo Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *****************************************************************************/

#include "modules/canbus_vehicle/x3epro/protocol/ads_brake_command_46.h"
#include "modules/drivers/canbus/common/byte.h"

namespace apollo {
namespace canbus {
namespace x3epro {

using ::apollo::drivers::canbus::Byte;

const int32_t Adsbrakecommand46::ID = 0x46;

// public
Adsbrakecommand46::Adsbrakecommand46() { Reset(); }

uint32_t Adsbrakecommand46::GetPeriod() const {
  // TODO(All) :  modify every protocol's period manually
  static const uint32_t PERIOD = 20 * 1000;
  return PERIOD;
}

void Adsbrakecommand46::Parse(const std::uint8_t* bytes, int32_t length,
                         X3epro* chassis) const {
  chassis->mutable_ads_brake_command_46()->set_drive_enable(drive_enable(bytes, length));
  chassis->mutable_ads_brake_command_46()->set_auto_brake_command(auto_brake_command(bytes, length));
  chassis->mutable_ads_brake_command_46()->set_auto_parking_command(auto_parking_command(bytes, length));
  chassis->mutable_ads_brake_command_46()->set_epb_rampauxiliarycommand(epb_rampauxiliarycommand(bytes, length));
  chassis->mutable_ads_brake_command_46()->set_forbidaebcommand(forbidaebcommand(bytes, length));
  chassis->mutable_ads_brake_command_46()->set_auto_drivercmd_alivecounter(auto_drivercmd_alivecounter(bytes, length));
  chassis->mutable_ads_brake_command_46()->set_auto_drivercmd_checksum(auto_drivercmd_checksum(bytes, length));
}

void Adsbrakecommand46::UpdateData(uint8_t* data) {
  set_p_drive_enable(data, drive_enable_);
  set_p_auto_brake_command(data, auto_brake_command_);
  set_p_auto_parking_command(data, auto_parking_command_);
  set_p_epb_rampauxiliarycommand(data, epb_rampauxiliarycommand_);
  set_p_forbidaebcommand(data, forbidaebcommand_);

  ++auto_drivercmd_alivecounter_;
  auto_drivercmd_alivecounter_ = (auto_drivercmd_alivecounter_) % 16;
  set_p_auto_drivercmd_alivecounter(data, auto_drivercmd_alivecounter_);

  auto_drivercmd_checksum_ =
      data[0] ^ data[1] ^ data[2] ^ data[3] ^ data[4] ^ data[5] ^ data[6];
  set_p_auto_drivercmd_checksum(data, auto_drivercmd_checksum_);
}

void Adsbrakecommand46::UpdateData_Heartbeat(uint8_t* data) {
  ++auto_drivercmd_alivecounter_;
  auto_drivercmd_alivecounter_ = (auto_drivercmd_alivecounter_) % 16;
  set_p_auto_drivercmd_alivecounter(data, auto_drivercmd_alivecounter_);

  auto_drivercmd_checksum_ =
      data[0] ^ data[1] ^ data[2] ^ data[3] ^ data[4] ^ data[5] ^ data[6];
  set_p_auto_drivercmd_checksum(data, auto_drivercmd_checksum_);
}

void Adsbrakecommand46::Reset() {
  // TODO(All) :  you should check this manually
  drive_enable_ = Ads_brake_command_46::DRIVE_ENABLE_INVALID;
  auto_brake_command_ = 0;
  auto_parking_command_ = Ads_brake_command_46::AUTO_PARKING_COMMAND_RELEASE;
  epb_rampauxiliarycommand_ = Ads_brake_command_46::EPB_RAMPAUXILIARYCOMMAND_INACTIVE;
  forbidaebcommand_ = Ads_brake_command_46::FORBIDAEBCOMMAND_NORMAL;
  auto_drivercmd_alivecounter_ = 0;
  auto_drivercmd_checksum_ = 0;
}

Adsbrakecommand46* Adsbrakecommand46::set_drive_enable(
    Ads_brake_command_46::Drive_enableType drive_enable) {
  drive_enable_ = drive_enable;
  return this;
 }

// config detail: {'bit': 0, 'enum': {0: 'DRIVE_ENABLE_INVALID', 1: 'DRIVE_ENABLE_VALID'}, 'is_signed_var': False, 'len': 1, 'name': 'Drive_Enable', 'offset': 0.0, 'order': 'motorola', 'physical_range': '[0|0]', 'physical_unit': '', 'precision': 1.0, 'type': 'enum'}
void Adsbrakecommand46::set_p_drive_enable(uint8_t* data,
    Ads_brake_command_46::Drive_enableType drive_enable) {
  int x = drive_enable;

  Byte to_set(data + 0);
  to_set.set_value(x, 0, 1);
}


Adsbrakecommand46* Adsbrakecommand46::set_auto_brake_command(
    int auto_brake_command) {
  auto_brake_command_ = auto_brake_command;
  return this;
 }

// config detail: {'bit': 23, 'is_signed_var': False, 'len': 8, 'name': 'AUTO_Brake_Command', 'offset': 0.0, 'order': 'motorola', 'physical_range': '[0|0]', 'physical_unit': '', 'precision': 1.0, 'type': 'int'}
void Adsbrakecommand46::set_p_auto_brake_command(uint8_t* data,
    int auto_brake_command) {
  auto_brake_command = ProtocolData::BoundedValue(0, 100, auto_brake_command);
  int x = auto_brake_command;

  Byte to_set(data + 2);
  to_set.set_value(x, 0, 8);
}


Adsbrakecommand46* Adsbrakecommand46::set_auto_parking_command(
    Ads_brake_command_46::Auto_parking_commandType auto_parking_command) {
  auto_parking_command_ = auto_parking_command;
  return this;
 }

// config detail: {'bit': 24, 'enum': {0: 'AUTO_PARKING_COMMAND_RELEASE', 1: 'AUTO_PARKING_COMMAND_APPLY'}, 'is_signed_var': False, 'len': 1, 'name': 'AUTO_Parking_Command', 'offset': 0.0, 'order': 'motorola', 'physical_range': '[0|0]', 'physical_unit': '', 'precision': 1.0, 'type': 'enum'}
void Adsbrakecommand46::set_p_auto_parking_command(uint8_t* data,
    Ads_brake_command_46::Auto_parking_commandType auto_parking_command) {
  int x = auto_parking_command;

  Byte to_set(data + 3);
  to_set.set_value(x, 0, 1);
}


Adsbrakecommand46* Adsbrakecommand46::set_epb_rampauxiliarycommand(
    Ads_brake_command_46::Epb_rampauxiliarycommandType epb_rampauxiliarycommand) {
  epb_rampauxiliarycommand_ = epb_rampauxiliarycommand;
  return this;
 }

// config detail: {'bit': 25, 'enum': {0: 'EPB_RAMPAUXILIARYCOMMAND_INACTIVE', 1: 'EPB_RAMPAUXILIARYCOMMAND_ACTIVE'}, 'is_signed_var': False, 'len': 1, 'name': 'EPB_RampAuxiliaryCommand', 'offset': 0.0, 'order': 'motorola', 'physical_range': '[0|0]', 'physical_unit': '', 'precision': 1.0, 'type': 'enum'}
void Adsbrakecommand46::set_p_epb_rampauxiliarycommand(uint8_t* data,
    Ads_brake_command_46::Epb_rampauxiliarycommandType epb_rampauxiliarycommand) {
  int x = epb_rampauxiliarycommand;

  Byte to_set(data + 3);
  to_set.set_value(x, 1, 1);
}


Adsbrakecommand46* Adsbrakecommand46::set_forbidaebcommand(
    Ads_brake_command_46::ForbidaebcommandType forbidaebcommand) {
  forbidaebcommand_ = forbidaebcommand;
  return this;
 }

// config detail: {'bit': 26, 'enum': {0: 'FORBIDAEBCOMMAND_NORMAL', 1: 'FORBIDAEBCOMMAND_FORBID'}, 'is_signed_var': False, 'len': 1, 'name': 'ForbidAebCommand', 'offset': 0.0, 'order': 'motorola', 'physical_range': '[0|0]', 'physical_unit': '', 'precision': 1.0, 'type': 'enum'}
void Adsbrakecommand46::set_p_forbidaebcommand(uint8_t* data,
    Ads_brake_command_46::ForbidaebcommandType forbidaebcommand) {
  int x = forbidaebcommand;

  Byte to_set(data + 3);
  to_set.set_value(x, 2, 1);
}


Adsbrakecommand46* Adsbrakecommand46::set_auto_drivercmd_alivecounter(
    int auto_drivercmd_alivecounter) {
  auto_drivercmd_alivecounter_ = auto_drivercmd_alivecounter;
  return this;
 }

// config detail: {'bit': 51, 'is_signed_var': False, 'len': 4, 'name': 'AUTO_DriverCmd_AliveCounter', 'offset': 0.0, 'order': 'motorola', 'physical_range': '[0|0]', 'physical_unit': '', 'precision': 1.0, 'type': 'int'}
void Adsbrakecommand46::set_p_auto_drivercmd_alivecounter(uint8_t* data,
    int auto_drivercmd_alivecounter) {
  auto_drivercmd_alivecounter = ProtocolData::BoundedValue(0, 15, auto_drivercmd_alivecounter);
  int x = auto_drivercmd_alivecounter;

  Byte to_set(data + 6);
  to_set.set_value(x, 0, 4);
}


Adsbrakecommand46* Adsbrakecommand46::set_auto_drivercmd_checksum(
    int auto_drivercmd_checksum) {
  auto_drivercmd_checksum_ = auto_drivercmd_checksum;
  return this;
 }

// config detail: {'bit': 63, 'is_signed_var': False, 'len': 8, 'name': 'AUTO_DriverCmd_CheckSum', 'offset': 0.0, 'order': 'motorola', 'physical_range': '[0|0]', 'physical_unit': '', 'precision': 1.0, 'type': 'int'}
void Adsbrakecommand46::set_p_auto_drivercmd_checksum(uint8_t* data,
    int auto_drivercmd_checksum) {
  auto_drivercmd_checksum = ProtocolData::BoundedValue(0, 255, auto_drivercmd_checksum);
  int x = auto_drivercmd_checksum;

  Byte to_set(data + 7);
  to_set.set_value(x, 0, 8);
}


Ads_brake_command_46::Drive_enableType Adsbrakecommand46::drive_enable(const std::uint8_t* bytes, int32_t length) const {
  Byte t0(bytes + 0);
  int32_t x = t0.get_byte(0, 1);

  Ads_brake_command_46::Drive_enableType ret =  static_cast<Ads_brake_command_46::Drive_enableType>(x);
  return ret;
}

int Adsbrakecommand46::auto_brake_command(const std::uint8_t* bytes, int32_t length) const {
  Byte t0(bytes + 2);
  int32_t x = t0.get_byte(0, 8);

  int ret = x;
  return ret;
}

Ads_brake_command_46::Auto_parking_commandType Adsbrakecommand46::auto_parking_command(const std::uint8_t* bytes, int32_t length) const {
  Byte t0(bytes + 3);
  int32_t x = t0.get_byte(0, 1);

  Ads_brake_command_46::Auto_parking_commandType ret =  static_cast<Ads_brake_command_46::Auto_parking_commandType>(x);
  return ret;
}

Ads_brake_command_46::Epb_rampauxiliarycommandType Adsbrakecommand46::epb_rampauxiliarycommand(const std::uint8_t* bytes, int32_t length) const {
  Byte t0(bytes + 3);
  int32_t x = t0.get_byte(1, 1);

  Ads_brake_command_46::Epb_rampauxiliarycommandType ret =  static_cast<Ads_brake_command_46::Epb_rampauxiliarycommandType>(x);
  return ret;
}

Ads_brake_command_46::ForbidaebcommandType Adsbrakecommand46::forbidaebcommand(const std::uint8_t* bytes, int32_t length) const {
  Byte t0(bytes + 3);
  int32_t x = t0.get_byte(2, 1);

  Ads_brake_command_46::ForbidaebcommandType ret =  static_cast<Ads_brake_command_46::ForbidaebcommandType>(x);
  return ret;
}

int Adsbrakecommand46::auto_drivercmd_alivecounter(const std::uint8_t* bytes, int32_t length) const {
  Byte t0(bytes + 6);
  int32_t x = t0.get_byte(0, 4);

  int ret = x;
  return ret;
}

int Adsbrakecommand46::auto_drivercmd_checksum(const std::uint8_t* bytes, int32_t length) const {
  Byte t0(bytes + 7);
  int32_t x = t0.get_byte(0, 8);

  int ret = x;
  return ret;
}
}  // namespace x3epro
}  // namespace canbus
}  // namespace apollo
